home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / filesy~1 / mfsdefrg.zoo / buffers.c next >
Encoding:
C/C++ Source or Header  |  1993-10-18  |  17.9 KB  |  671 lines

  1. /*
  2.  * buffers.c - buffer management for the Linux file system degragmenter.
  3.  * buffers.c,v 1.5 1993/01/07 14:48:47 linux Exp
  4.  *
  5.  * Copyright (C) 1992, 1993 Stephen Tweedie (sct@dcs.ed.ac.uk)
  6.  * 
  7.  * This file may be redistributed under the terms of the GNU General
  8.  * Public License.
  9.  *
  10.  */
  11.  
  12. /* Modified for Minixfs/MiNT by S N Henson 1992 */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "defrag.h"
  19.  
  20. #define sizeof_buffer (sizeof(*pool) + block_size)
  21. #define buffer(i) ((Buffer *) \
  22.              (((char *) pool) + (i) * sizeof_buffer))
  23.  
  24. /* The buffer pool is a unified buffer space containing both the
  25.    pending pool and the rescue pool.  The pending pool holds buffers
  26.    waiting to be written to disk as part of the sequential write of 
  27.    optimised zones; the rescue pool holds the contents of zones about
  28.    to be overwritten by blocks from the write pool.
  29.  
  30.    The select set is an arbitrary subset of the whole buffer pool.
  31.    This feature is used in various places to select a group of buffers
  32.    for reading or writing. 
  33.  
  34.    The buffer pool and hash table will not necessarily be totally in
  35.    synch with the relocation maps d2n_map and n2d_map.  It is possible
  36.    for a buffer to exist for a block which, according to the
  37.    relocation maps, is still on disc.  This is because the relocation
  38.    maps are NOT modified by the creation or deletion of pool buffers,
  39.    but only by the actual reading or writing of those pool buffers.
  40.    (The basis of the defragmenter's optimisation is the deferring as
  41.    long as possible of reads and writes, and the sorting of bulk
  42.    reads/writes to improve performance.)
  43. */
  44.  
  45. int pool_size = 512;
  46. Buffer *pool;
  47. Buffer *first_free_buffer;
  48. Buffer **select_set;
  49. int select_set_size;
  50. int free_buffers, count_output_buffers, count_rescue_buffers;
  51.  
  52. int count_buffer_writes = 0, count_buffer_reads = 0;
  53. int count_write_groups = 0, count_read_groups = 0;
  54. int count_buffer_migrates = 0, count_buffer_forces = 0;
  55. int count_buffer_read_aheads = 0;
  56.  
  57. /* We will hash buffered blocks on the least significant bits of the
  58.    block's dest_zone */
  59. #define HASH_SIZE 1024
  60. static Buffer * (hash[HASH_SIZE]) = {0};
  61. #define hash_list(zone) (hash[(zone) % (HASH_SIZE)])
  62.  
  63. /* First of all, the primitive buffer management functions: allocation
  64.    and freeing of buffer blocks. */
  65.  
  66. /* Set up the buffer tables and clear the hash table.  This must be
  67.    called after the fs-dependent code has been initialised (typically
  68.    in read_tables() ) so that the block size variables have been
  69.    correctly initialised. */
  70. void init_buffer_tables()
  71. {
  72.     int i;
  73.     if (debug)
  74.         printf ("DEBUG: init_buffer_tables()\n");
  75.     
  76.     for (i=0; i<HASH_SIZE; i++)
  77.         hash[i] = 0;
  78.     pool = (Buffer *) malloc (pool_size * sizeof_buffer);
  79.     if (!pool)
  80.         die ("Unable to allocate buffer pool.");
  81.     memset (pool, 0, pool_size * sizeof_buffer);
  82.     select_set = (Buffer **) malloc (pool_size *
  83.                      sizeof(*select_set));
  84.     if (!select_set)
  85.         die ("Unable to allocate pool select set");
  86.     select_set_size = 0;
  87.     /* Set up the free buffer list */
  88.     for (i=0; i<pool_size-1; i++)
  89.     {
  90.         buffer(i)->next = buffer(i+1);
  91.     }
  92.     buffer(pool_size-1)->next = 0;
  93.     first_free_buffer = buffer(0);
  94.     free_buffers = pool_size;
  95.     count_output_buffers = count_rescue_buffers = 0;
  96. }
  97.  
  98. /* Lookup a block in the hash table.  Returns a pointer to the entry
  99.    in the hash list (ie. doubly indirected), or zero. */
  100. static Buffer ** hash_lookup (Block zone)
  101. {
  102.     Buffer ** b;
  103.     b = &(hash_list(zone));
  104.     while (*b)
  105.     {
  106.         if ((*b)->dest_zone == zone)
  107.             return b;
  108.         b = &((*b)->next);
  109.     }
  110.     return 0;
  111. }
  112.  
  113. /* Allocate an unused buffer from the pool. */
  114. Buffer * allocate_buffer (Block zone, BufferType btype)
  115. {
  116.     Buffer * b;
  117.     if (!first_free_buffer)
  118.         die ("No free buffers");
  119.     assert (free_buffers);
  120.     assert (!(hash_lookup(zone)));
  121.  
  122.     /* Remove a buffer from the free list */
  123.     b = first_free_buffer;
  124.     first_free_buffer = first_free_buffer->next;
  125.     assert (!b->in_use);
  126.     b->in_use = 1;
  127.     
  128.     /* Set up the buffer fields */
  129.     b->dest_zone = zone;
  130.     b->btype = btype;
  131.     b->full = 0;
  132.  
  133.     /* Update buffer counts */
  134.     free_buffers--;
  135.     switch (btype)
  136.     {
  137.     case OUTPUT:
  138.         count_output_buffers++;
  139.         break;
  140.     case RESCUE:
  141.         count_rescue_buffers++;
  142.         break;
  143.     }
  144.         
  145.     /* Link the buffer into the hash table */
  146.     b->next = hash_list(zone);
  147.     hash_list(zone) = b;
  148.  
  149.     assert ((count_rescue_buffers + count_output_buffers +
  150.          free_buffers) == pool_size);
  151.     return b;
  152. }
  153.  
  154. /* Free up a buffer from the buffer pool.  Manage the free buffer list
  155.    and hash table appropriately. */
  156. void free_buffer (Buffer *b)
  157. {
  158.     Buffer **p;
  159.     if (debug)
  160.         printf ("DEBUG: free_buffer (%lu)\n", b->dest_zone);
  161.     assert (b->in_use);
  162.     assert (first_free_buffer ? free_buffers : !free_buffers);
  163.     /* Assert : throwing away a buffer's data is illegal unless 
  164.        the zone is on disk somewhere. */ 
  165.     assert (n2d(b->dest_zone));
  166.     b->in_use = 0;
  167.  
  168.     /* Unlink this buffer from the hash table */
  169.     p = hash_lookup (b->dest_zone);
  170.     assert (p);
  171.     assert ((*p) == b);
  172.     *p = b->next;
  173.     
  174.     /* Link the buffer into the free list */
  175.     b->next = first_free_buffer;
  176.     first_free_buffer = b;
  177.  
  178.     /* Update buffer counts */
  179.     free_buffers++;
  180.     switch (b->btype)
  181.     {
  182.     case OUTPUT:
  183.         count_output_buffers--;
  184.         break;
  185.     case RESCUE:
  186.         count_rescue_buffers--;
  187.         break;
  188.     }
  189. }
  190.  
  191. /* Set a buffer's type - used to migrate blocks from the RESCUE to the 
  192.    OUTPUT pool. */
  193. void set_buffer_type (Buffer *b, BufferType btype)
  194. {
  195.     if (debug)
  196.         printf ("DEBUG: set_buffer_type (%lu:%d, %d)\n",
  197.             b->dest_zone, b->btype, btype);
  198.     if (b->btype == btype)
  199.         return;
  200.     switch (b->btype)
  201.     {
  202.     case OUTPUT:
  203.         count_output_buffers--;
  204.         break;
  205.     case RESCUE:
  206.         count_rescue_buffers--;
  207.         break;
  208.     }
  209.     b->btype = btype;
  210.     switch (btype)
  211.     {
  212.     case OUTPUT:
  213.         count_output_buffers++;
  214.         break;
  215.     case RESCUE:
  216.         count_rescue_buffers++;
  217.         break;
  218.     }
  219.     assert ((count_rescue_buffers + count_output_buffers +
  220.          free_buffers) == pool_size);
  221. }
  222.  
  223.         
  224. /* Select a group of buffers based on an arbitrary selection predicate */
  225. void select_buffers (int (*fn) (const Buffer *))
  226. {
  227.     int i;
  228.     if (debug)
  229.         printf ("DEBUG: select_buffers()\n");
  230.     
  231.     select_set_size = 0;
  232.     for (i=0; i<pool_size; i++)
  233.     {
  234.         if (buffer(i)->in_use && fn(buffer(i)))
  235.             select_set[select_set_size++] = buffer(i);
  236.     }
  237.     if (debug)
  238.         printf ("DEBUG: selected %d buffers\n", select_set_size);
  239. }
  240.  
  241. /* Compare two buffers based on their dest_zone fields, for use by
  242.    the stdlib qsort() function */
  243. static int compare_buffer_zones(const void *a, const void *b)
  244. {
  245.     Block azone, bzone;
  246.     azone = (*((Buffer **) a))->dest_zone;
  247.     bzone = (*((Buffer **) b))->dest_zone;
  248.     
  249.     if (azone < bzone)
  250.         return -1;
  251.     if (azone == bzone)
  252.         return 0;
  253.     return 1;
  254. }
  255.  
  256. /* Compare two buffers again, this time based on their source zone */
  257. static int compare_buffer_zones_for_read (const void *a, const void *b)
  258. {
  259.     Block azone, bzone;
  260.     azone = n2d((*((Buffer **) a))->dest_zone);
  261.     bzone = n2d((*((Buffer **) b))->dest_zone);
  262.     
  263.     if (azone < bzone)
  264.         return -1;
  265.     if (azone == bzone)
  266.         return 0;
  267.     return 1;
  268. }
  269.  
  270. /* Sort the current select_set based on buffer dest_zone.  Sorting
  271.    buffers in this manner before a read or write will significantly
  272.    improve i/o performance, but is not essential for correct running
  273.    of the program. */
  274. void sort_select_set (void)
  275. {
  276.     if (debug)
  277.         printf ("DEBUG: sort_select_set()\n");
  278.     qsort (select_set, select_set_size, sizeof (*select_set), 
  279.            compare_buffer_zones);
  280. }
  281.  
  282. /* Perform a similar sort, but sort on source zones for an order
  283.    suitable for reading the select set. */
  284. void sort_select_set_for_read (void)
  285. {
  286.     if (debug)
  287.         printf ("DEBUG: sort_select_set_for_read()\n");
  288.     qsort (select_set, select_set_size, sizeof (*select_set), 
  289.            compare_buffer_zones_for_read);
  290. }
  291.  
  292. /*
  293.  * check_zone_nr checks to see that *nr is a valid zone nr. It dies if
  294.  * it isn't.
  295.  */
  296. void check_zone_nr (Block nr)
  297. {
  298.     if (debug)
  299.         printf ("DEBUG: check_zone_nr (&%d)\n", nr);
  300.     if (nr < first_zone)
  301.         printf ("Zone nr %d < first_zone.\n", nr);
  302.     else if (nr >= zones)
  303.         printf ("Zone nr %d > zones.\n", nr);
  304.     else
  305.         return;
  306.     die ("Invalid zone number");
  307. }
  308.  
  309. /*
  310.  * read_current_block reads block nnr into the buffer at addr.
  311.  */
  312. void read_current_block (Block nnr, char * addr)
  313. {
  314.     if (debug)
  315.         printf ("DEBUG: read_block (&%d, %d)\n", nnr, (int) addr);
  316.     if (!nnr)
  317.         return;
  318.     check_zone_nr(nnr);
  319.  
  320.     if (block_size * (nnr) != lseek (IN, block_size * (nnr), SEEK_SET))
  321.         die ("seek failed in read_block");
  322.     if (block_size != read (IN, addr, block_size))
  323.         die ("Read error: bad block in read_block.");
  324. }
  325.  
  326. /*
  327.  * write_current_block writes block nr to disk.
  328.  */
  329. void write_current_block (Block nnr, char * addr)
  330. {
  331.     int r;
  332.     
  333.     if (debug)
  334.         printf ("DEBUG: write_block(%d, %d)\n", nnr, (int) addr);
  335.     check_zone_nr(nnr);
  336.     if (block_size * nnr != lseek (IN, block_size * nnr, SEEK_SET))
  337.         die ("seek failed in write_block");
  338.     if (readonly)
  339.         return;
  340.     if (block_size != (r = write (IN, addr, block_size)))
  341.         printf ("Write error: bad block (%d,%d) in write_block.\n", nnr, r);
  342.     if (blocks_until_sync++ >= SYNC_PERIOD)
  343.     {
  344.         sync();
  345.         blocks_until_sync = 0;
  346.     }
  347. }
  348.  
  349. /* read/write_old_block function as read_block and write_block, but using
  350.    the zone map to follow blocks which may have been swapped to make room for
  351.    optimised zones. */
  352. void read_old_block (Block onr, char *addr)
  353. {
  354.     check_zone_nr(onr);
  355.     read_current_block (d2n(onr), addr);
  356. }
  357.  
  358. void write_old_block (Block onr, char *addr)
  359. {
  360.     check_zone_nr(onr);
  361.     write_current_block (d2n(onr), addr);
  362. }
  363.  
  364.  
  365. /* Read/write _Buffer_s.  The read/write_old/new_block routines work
  366.    on arbitrary blocks and arbitrary memory locations; the Buffer
  367.    read/write routines, on the other hand, interact fully with the
  368.    zone relocation maps and Buffer data structures from the buffer
  369.    pool. */
  370.  
  371. void read_buffer_data (Buffer *b)
  372. {
  373.     Block source;
  374.     if (debug)
  375.         printf ("DEBUG: read_buffer_data (%lu)\n",
  376.             b->dest_zone);
  377.     assert (b->in_use);
  378.     if (b->full)
  379.         return;
  380.     source = n2d(b->dest_zone);
  381.     /* Don't bother reading here if we are in readonly mode; there
  382.        will be no need to write it back at any time. */
  383.     if (!readonly)
  384.         read_current_block (source, b->data);
  385.     d2n(source) = 0;
  386.     n2d(b->dest_zone) = 0;
  387.     b->full = 1;
  388.     count_buffer_reads++;
  389.     return;
  390. }
  391.  
  392. void write_buffer_data_at (Buffer *b, Block dest)
  393. {
  394.     if (debug)
  395.         printf ("DEBUG: write_buffer_data_at (%lu, %lu)\n", 
  396.             b->dest_zone, dest);
  397.     assert (b->in_use & b->full);
  398.     write_current_block (dest, b->data);
  399.     assert (!n2d(b->dest_zone));
  400.     assert (!d2n(dest));
  401.     d2n(dest) = b->dest_zone;
  402.     n2d(b->dest_zone) = dest;
  403.     count_buffer_writes++;
  404. }
  405.  
  406. void write_buffer_data (Buffer *b)
  407. {
  408.     write_buffer_data_at (b, b->dest_zone);
  409. }
  410.  
  411.  
  412. /***********************************************************************
  413.    The disk relocation routines: the working core of the defragmenter.
  414.    These routines are responsible for implementing the disk block
  415.    relocation defined by the forward and reverse relocation maps
  416.    d2n_map and n2d_map.
  417.  ***********************************************************************/
  418.     
  419. void read_select_set ()
  420. {
  421.     int i;
  422.     sort_select_set_for_read();
  423.     if (!select_set_size)
  424.         return;
  425.     if (verbose>1)
  426.         printf ("Reading %d blocks...\n", select_set_size);
  427.     for (i=0; i < select_set_size; i++)
  428.     {
  429.         assert (!select_set[i]->full);
  430.         read_buffer_data (select_set[i]);
  431.     }
  432.     count_read_groups++;
  433. }
  434.  
  435. void write_select_set ()
  436. {
  437.     int i;
  438.     sort_select_set();
  439.     if (!select_set_size)
  440.         return;
  441.     if (verbose>1)
  442.         printf ("Writing %d blocks...\n", select_set_size);
  443.     for (i=0; i < select_set_size; i++)
  444.     {
  445.         assert (select_set[i]->in_use &&
  446.             select_set[i]->full);
  447.         write_buffer_data(select_set[i]);
  448.     }
  449.     count_write_groups++;
  450. }
  451.  
  452. void free_select_set()
  453. {
  454.     int i;
  455.     for (i=0; i < select_set_size; i++)
  456.         free_buffer (select_set[i]);
  457.     select_set_size = 0;
  458. }
  459.  
  460. /* A few useful buffer selection predicates... */
  461. int output_buffer_p (const Buffer *b)
  462. {
  463.     return (b->btype == OUTPUT);
  464. }
  465.  
  466. int empty_buffer_p (const Buffer *b)
  467. {
  468.     return (!b->full);
  469. }
  470.  
  471. int rescue_buffer_p (const Buffer *b)
  472. {
  473.     return (b->btype == RESCUE);
  474. }
  475.  
  476. int true (const Buffer *b)
  477. {
  478.     return 1;
  479. }
  480.  
  481.  
  482. /* Routines for reading and flushing data */
  483. void read_all_buffers()
  484. {
  485.     /* Select and sort all (non-empty) buffers for reading */
  486.     select_buffers (empty_buffer_p);
  487.     read_select_set();
  488. }    
  489.  
  490. void flush_output_pool()
  491. {
  492.     read_all_buffers ();
  493.     select_buffers (output_buffer_p);
  494.     if (!select_set_size)
  495.         return;
  496.     if (verbose>1)
  497.         printf ("Saving: ");
  498.     write_select_set();
  499.     free_select_set();
  500. }
  501.  
  502. /* Here we employ various methods for getting rid of some of the
  503.    buffers in the buffer pool.  There are three methods used, in
  504.    order of preference:
  505.    flush output buffers: Buffers waiting to be sequentially written to
  506.         disk are written now.
  507.    If that fails to free more than 25% of buffers:
  508.    flush rescue buffers: Rescue buffers whose destination zones are
  509.            empty (ie. already moved into the output pool and
  510.         hence to disk) are flushed, by migrating them
  511.         immediately to the output pool.
  512.    If that fails to free more than 20% of the buffers, drastic action
  513.    is necessary:
  514.    force rescue buffers: Rescue buffers are sorted, and a number of
  515.         rescue buffers destined for later disk zones are
  516.         written to free space at the end of the disk (NOT to
  517.         their ultimate destinations, though).  We choose later
  518.         rescue buffers to flush in preference to earlier
  519.         buffers, in anticipation of soon being able to migrate
  520.         early rescued blocks to the output pool. */
  521. void get_some_buffer_space()
  522. {
  523.     int i, count = 0;
  524.     Block dest;
  525.     
  526.     flush_output_pool();
  527.     if ((free_buffers * 4) > pool_size)
  528.         return;
  529.  
  530.     /* OK, try migrating rescue buffers to the output pool */
  531.     for (i=0; i<pool_size; i++)
  532.     {
  533.         if (buffer(i)->in_use &&
  534.             buffer(i)->btype == RESCUE &&
  535.             d2n(buffer(i)->dest_zone) == 0)
  536.         {
  537.             set_buffer_type(buffer(i), OUTPUT);
  538.             count++;
  539.             count_buffer_migrates++;
  540.         }
  541.     }
  542.     if (verbose>1)
  543.         printf ("Migrated %d rescued blocks%s", count,
  544.             count ? ": " : ".\n");
  545.     flush_output_pool();
  546.     if ((free_buffers * 5) > pool_size)
  547.         return;
  548.     
  549.     /* Serious trouble - more than 80% of the pool is occupied by 
  550.        unsaveable rescue buffers.  We'll have to force some of 
  551.        them to disk.  (The algorithm I'm using tries hard to avoid
  552.        this, since it is the only occasion where a disk block may
  553.        have to be be moved more than once during relocation.) */
  554.     select_buffers (true);
  555.     sort_select_set ();
  556.     assert (select_set_size + free_buffers == pool_size);
  557.     /* Select the top 25% of rescue buffers for forcing (this is 
  558.        an entirely arbitrary number; in fact, most of the numbers 
  559.        in this function could probably be tweaked to tune 
  560.        performance, but these seem to work OK. */
  561.     dest = zones-1; count = 0;
  562.     if (verbose>1)
  563.         printf ("Pool too full - forcing buffers...");
  564.     for (i = select_set_size-1; i >= ((select_set_size*3)/4); i--)
  565.     {
  566.         while (d2n(dest))
  567.             dest--;
  568.         write_buffer_data_at (select_set[i], dest);
  569.         free_buffer (select_set[i]);
  570.         count_buffer_forces++;
  571.         count++;
  572.     }
  573.     if (verbose>1)
  574.         printf (" %d buffers recovered.\n", count);
  575.     select_set_size = 0;
  576. }
  577.  
  578. void remap_disk_blocks (void)
  579. {
  580.     Block source, dest = first_zone, dest2;
  581.     Buffer **p;
  582.  
  583.     if (debug)
  584.         printf ("DEBUG: remap_disk_blocks()\n");
  585.     if (verbose)
  586.         printf ("Relocating disk blocks - this could take a while.\n");
  587.     
  588.     /* Walk through each disk block sequentially, rescuing 
  589.        previous contents and reading the new contents into the 
  590.        output buffer. */
  591.     do
  592.     {
  593.         if (verbose && !(dest & 1023))
  594.         {
  595.             printf ("Relocating : %d MB...\n",
  596.                 (dest / 1024));
  597.         }
  598.         
  599.         /* Don't try to save stuff to disk until we are */
  600.         /* running out of free buffers. */
  601.         if (free_buffers < 4)
  602.         {
  603.             get_some_buffer_space ();
  604.         }
  605.         assert (free_buffers >= 4);
  606.         
  607.         /* Use the reverse relocation map to obtain the block 
  608.            which should go in this space */
  609.         source = n2d(dest);
  610.         /* Zero means this block cannot be found on disk.
  611.            Either it should remain empty (it may  be a bad
  612.            block), or it has already been read into the rescue
  613.            pool.  If source==dest, the block is already in
  614.            place. */
  615.         if (source == dest)
  616.             continue;
  617.         /* We may have already read the source block into the 
  618.            rescue pool; look for the block (indexed by dest) 
  619.            in the hash table */
  620.         p = hash_lookup (dest);
  621.         if (p)
  622.         {
  623.             /* Yes, we already have the block so make it 
  624.                an OUTPUT buffer (it must currently be a 
  625.                RESCUE buffer). */
  626.             assert ((*p)->btype == RESCUE);
  627.             set_buffer_type (*p, OUTPUT);
  628.             count_buffer_read_aheads++;
  629.         }
  630.         else
  631.         {
  632.             /* No, the block is not in the hash table:
  633.                if the block can be found on disk, read it;
  634.                otherwise, the block will remain empty and
  635.                can be skipped. */
  636.             if (!source)
  637.                 continue;
  638.             allocate_buffer (dest, OUTPUT);
  639.         }
  640.         
  641.         /* Rescue the block about to be overwritten.  All 
  642.            buffers are referred to by their destination zone 
  643.            (according to the relocation map), NOT the zone 
  644.            that block is currently residing in.  So, work out 
  645.            the final destination of the block currently 
  646.            residing in the destination zone by looking up the 
  647.            forward relocation map. */
  648.         dest2 = d2n(dest);
  649.         /* Zero means that the dest block may be safely 
  650.            overwritten. */
  651.         if (!dest2)
  652.             continue;
  653.         /* Check to see if we have already read this block */
  654.         p = hash_lookup (dest2);
  655.         if (p)
  656.         {
  657.             /* We have, so no need to read it again */
  658.             continue;
  659.         }
  660.         /* Read the block into the rescue pool */
  661.         allocate_buffer (dest2, RESCUE);
  662.     } while (++dest < zones);
  663.     /* We have got to the end, so flush any remaining buffers. */
  664.     flush_output_pool ();
  665.     assert (!count_output_buffers);
  666.     assert (!count_rescue_buffers);
  667.     assert (free_buffers == pool_size);
  668. }
  669.  
  670.        
  671.